import api from '../../api';
import { message } from 'antd';

export default {
  namespace: 'keyFiguresM',
  state: {
    fixedIncomeColumns: [
      //固定收益类表头
      {
        title: '产品名称',
        dataIndex: 'productName',
        key: 'productName',
        width: 120,
        render: (val, record) => {
          return record.productCode + '-' + val;
        },
      },
      {
        title: '未来一日净现金流(万元)',
        dataIndex: 'riseFall',
        key: 'riseFall',
        width: 180,
        align: 'right',
        sorter: (a, b) => a.riseFall - b.riseFall,
      },
      {
        title: '静态收益率',
        dataIndex: 'unitEstimation',
        key: 'unitEstimation',
        width: 120,
        align: 'right',
        sorter: (a, b) => a.unitEstimation - b.unitEstimation,
      },
      {
        title: '纯债久期',
        dataIndex: 'shareStocks',
        key: 'shareStocks',
        width: 120,
        align: 'right',
        sorter: (a, b) => a.shareStocks - b.shareStocks,
      },
      {
        title: '债券持仓占比',
        dataIndex: 'bondPositions',
        key: 'bondPositions',
        width: 120,
        align: 'right',
        sorter: (a, b) => a.bondPositions - b.bondPositions,
      },
    ],
    equityColumns: [
      //权益类表头
      {
        title: '产品名称',
        dataIndex: 'productName',
        key: 'productName',
        width: 120,
        render: (val, record) => {
          return record.productCode + '-' + val;
        },
      },
      {
        title: '实时日盈亏',
        dataIndex: 'todayPnl',
        key: 'todayPnl',
        type: 'updownText',
        width: 120,
        percent: true,
        sorter: (a, b) => a.todayPnl - b.todayPnl,
        render: (_) => (_ || _ === 0 ? <span style={_ == 0 ? { color: 'black' } : _ > 0 ? { color: 'red' } : { color: 'green' }}>{_?.toFixed(2)}</span> : '--'),
      },
      {
        title: '实时涨跌幅',
        dataIndex: 'riseFall',
        key: 'riseFall',
        type: 'updownText',
        width: 120,
        percent: true,
        sorter: (a, b) => a.riseFall - b.riseFall,
        render: (_) => (_ || _ === 0 ? <span style={_ == 0 ? { color: 'black' } : _ > 0 ? { color: 'red' } : { color: 'green' }}>{(_ * 100)?.toFixed(2) + '%'}</span> : '--'),
      },

      {
        title: '单位净值估算',
        dataIndex: 'unitEstimation',
        key: 'unitEstimation',
        width: 120,
        type: 'updownText',
        sorter: (a, b) => a.unitEstimation - b.unitEstimation,
        render: (_) => (_ || _ === 0 ? _?.toFixed(4) : '--'),
      },
      {
        title: '股票实时占比',
        dataIndex: 'shareStocks',
        key: 'shareStocks',
        type: 'updownText',
        width: 120,
        percent: true,
        sorter: (a, b) => a.shareStocks - b.shareStocks,
        render: (_) => (_ || _ == 0 ? _?.toFixed(2) + '%' : '--'),
      },
    ],
    commoditiesColumns: [
      //商品及衍生品类表头
      {
        title: '产品名称',
        dataIndex: 'productName',
        key: 'productName',
        width: 120,
        render: (val, record) => {
          return record.productCode + '-' + val;
        },
      },
      {
        title: '实时日盈亏',
        dataIndex: 'todayPnl',
        key: 'todayPnl',
        type: 'updownText',
        width: 120,
        percent: true,
        sorter: (a, b) => a.todayPnl - b.todayPnl,
        render: (_) => (_ || _ === 0 ? <span style={_ == 0 ? { color: 'black' } : _ > 0 ? { color: 'red' } : { color: 'green' }}>{_?.toFixed(2)}</span> : '--'),
      },
      {
        title: '实时涨跌幅',
        dataIndex: 'riseFall',
        key: 'riseFall',
        type: 'updownText',
        width: 120,
        percent: true,
        sorter: (a, b) => a.riseFall - b.riseFall,
        render: (_) => (_ || _ === 0 ? <span style={_ == 0 ? { color: 'black' } : _ > 0 ? { color: 'red' } : { color: 'green' }}>{(_ * 100)?.toFixed(2) + '%'}</span> : '--'),
      },
      {
        title: '单位净值估算',
        dataIndex: 'unitEstimation',
        key: 'unitEstimation',
        width: 120,
        type: 'updownText',
        sorter: (a, b) => a.unitEstimation - b.unitEstimation,
        render: (_) => (_ || _ === 0 ? _?.toFixed(4) : '--'),
      },
      {
        title: '衍生品实时占比',
        dataIndex: 'proportionDerivatives',
        key: 'proportionDerivatives',
        type: 'updownText',
        width: 120,
        percent: true,
        sorter: (a, b) => a.proportionDerivatives - b.proportionDerivatives,
        render: (_) => (_ || _ === 0 ? _?.toFixed(2) + '%' : '--'),
      },
    ],
    fixedIncomeData: [], //固定收益类数据
    equityData: [], //权益类数据
    commoditiesData: [], //商品及衍生品类数据
    userViewProduct: {},
    riseFallProduct: {},
    unitEstimationProduct: {},
    bondPositionsProduct: {},
  },
  effects: {
    // 获取固定收益类数据
    *getFixedIncomeData({ payload, callback }, { call, put }) {
      try {
        const response = yield call(api.keyFigures.getFixedIncomeData, payload);
        // const response = { code: 0, data: [{ productName: 'zht', productCode: '123', productId: 'fbae4e7f10a744e4a383a26b8966acbf', riseFall: 3, unitEstimation: 0.03, shareStocks: 3, bondPositions: 0.03 }] }
        if (response?.code == 0) {
          yield put({
            type: 'setState',
            payload: {
              fixedIncomeData: response.data,
            },
          });
          return response.data;
        } else {
          message.error(response.msg || '获取固定收益类数据失败');
          return [];
        }
      } catch (e) {
        console.log(e);
      }
    },
    // 获取权益类数据
    *getEquityData({ payload, callback }, { call, put }) {
      try {
        const response = yield call(api.keyFigures.getEquityData, payload);
        // const response = { code: 0, data: [
        //   { productName: 'WXNSH', productCode: '111444666633', productId: 'ff129d29277c4d8ab002b3ec1d99e51d', isSimulate: '0',
        //   riseFall:0.03,unitEstimation:100,shareStocks:-0.02 },
        // { productName: '20221116', productCode: '20221116',riseFall:0.03,unitEstimation:100,shareStocks:-0.02 }] }
        if (response?.code == 0) {
          yield put({
            type: 'setState',
            payload: {
              equityData: response.data,
            },
          });
          return response.data;
        } else {
          message.error(response.msg || '获取权益类数据失败');
          return [];
        }
      } catch (e) {
        console.log(e);
      }
    }, // 获取商品及衍生品类数据
    *getCommoditiesData({ payload, callback }, { call, put }) {
      try {
        const response = yield call(api.keyFigures.getCommoditiesData, payload);
        // const response = { code: 0, data: [{ productName: 'zht', productCode: '123',riseFall:0.03,unitEstimation:100,proportionDerivatives:-0.02 },
        // { productName: 'zht', productCode: '123' ,riseFall:0.03,unitEstimation:100,proportionDerivatives:-0.02},
        //  { productName: 'zht', productCode: '123' ,riseFall:0.03,unitEstimation:100,proportionDerivatives:-0.02}] }
        if (response?.code == 0) {
          yield put({
            type: 'setState',
            payload: {
              commoditiesData: response.data,
            },
          });
          return response.data;
        } else {
          message.error(response.msg || '获取商品及衍生品类数据失败');
          return [];
        }
      } catch (e) {
        console.log(e);
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
